package com.ruoyi.web.controller.erp;

import com.bonuo.erp.domain.Supplier;
import com.bonuo.erp.service.SupplierService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 供应商管理Controller
 * 
 * @author doubao
 */
@Api(tags = "供应商管理")
@RestController
@RequestMapping("/erp/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 查询供应商列表
     */
    @ApiOperation(value = "查询供应商列表",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @ApiOperation(value = "导出供应商列表",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Supplier supplier) {
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 新增供应商
     */
    @ApiOperation(value = "新增供应商",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Supplier supplier) {
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改供应商
     */
    @ApiOperation(value = "修改供应商",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody Supplier supplier) {
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除供应商
     */
    @ApiOperation(value = "删除供应商",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return toAjax(supplierService.deleteSupplierById(id));
    }

    /**
     * 导入供应商数据
     */
    @ApiOperation(value = "导入供应商数据",httpMethod = "POST")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        List<Supplier> supplierList = util.importExcel(file.getInputStream());
        String message = supplierService.importSupplier(supplierList, updateSupport,getUsername());
        return success(message);
    }

    /**
     * 下载供应商导入模板
     */
    @ApiOperation(value = "下载供应商导入模板",httpMethod = "GET")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Supplier> util = new ExcelUtil<>(Supplier.class);
        util.exportExcel(response, null, "供应商导入模板");
    }
}